package com.achievo.know.domain.infra.hibernate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.achievo.know.domain.Role;
import com.achievo.know.domain.RoleRepository;
import com.dayatang.spring.repository.BaseEntityRepositoryHibernateSpring;

@Repository
public class RoleRepositoryImpl extends BaseEntityRepositoryHibernateSpring<Role,Long> implements RoleRepository {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}


	public RoleRepositoryImpl() {
		super(Role.class);
	}


	@Override
	public void batchDelete(final String[] ids) {
		final String sql="delete from rams_role where id=?";
		final String delUserRoleSql="delete from rams_user_r_role where ROLE_ID=?";
		final String delRoleFuncSql="delete from rams_role_r_function where ROLE_ID=?";
		BatchPreparedStatementSetter bss=new BatchPreparedStatementSetter(){

			@Override
			public int getBatchSize() {
				return ids.length;
			}

			@Override
			public void setValues(PreparedStatement ps, int i)
					throws SQLException {
					ps.setLong(1, Long.parseLong(ids[i]));
			}
			
		};
		jdbcTemplate.batchUpdate(delUserRoleSql, bss);
		jdbcTemplate.batchUpdate(delRoleFuncSql, bss);
		jdbcTemplate.batchUpdate(sql, bss);
	}


	@Override
	public List<Role> getAll() {
		return findAll();
	}
	

}
